﻿using System;
using System.Diagnostics;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;

namespace Acoris.Document
{
    using Acoris.Foundation;
    using Acoris.Exceptions;

#if NETFX_CORE || WINDOWS_UWP
    using Windows.Storage;
    using Windows.Storage.Pickers;
#elif NETFX_DESKTOP || WINDOWS_WPF
    using System.IO;
#endif
    /// <summary>
    /// 表示一个标记文档
    /// </summary>    
    public sealed class AcorisMarkdownDocument:INotifyPropertyChanged
    {

        //---------------------------------------------------------------------------------------------------
        //  静态文档层模型
        //--------------------------------------------------------------------------------------------------- 

        // Flat String -> ActiveDocument(Tree like) -> Layout-Tree -> Rendering-Tree
        //                      |  |
        //                      |  |---------------------------|
        //                      |-->User Pointer--Operation----|

        #region Fields

        AcorisMarkdownHeader                _header;
        AcorisMarkdownResourceManager       _resource;
        AcorisMarkdownContent               _content;
        AcorisMarkdownInteractionManager    _interaction;

        #endregion

        public AcorisMarkdownDocument()
        {
            _resource = new AcorisMarkdownResourceManager();
            _interaction = new AcorisMarkdownInteractionManager();
        }

        #region Method

        /// <summary>
        /// 格式化原始文档
        /// </summary>
        /// <param name="amlDocument"></param>
        public void LoadAml(string amlDocument)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Content)));
        }

        public void Acitve()
        {

        }

        /// <summary>
        /// 异步加载文档
        /// </summary>
        /// <param name="fileName"></param>
        async public void Load(string fileName)
        {
#if NETFX_CORE || WINDOWS_UWP
            try {
                StorageFile file = await StorageFile.GetFileFromPathAsync(fileName);
                var document = await FileIO.ReadTextAsync(file);
                LoadAml(document);
            }
            catch {
                throw new CannotOpenFileException();
            }
#elif NETFX_DESKTOP || WINDOWS_WPF
            try{
                var stream = File.OpenRead(fileName);
                var sr = new StreamReader(stream);
                var document = await sr.ReadToEndAsync();
                LoadAml(document);
            }catch(FileNotFoundException){
                throw new CannotOpenFileException();
            }
#endif
        }

        async public void Save(string fileName)
        {

        }

        async public void Save(
#if NETFX_CORE || WINDOWS_UWP
            StorageFile file
#elif NETFX_DESKTOP || WINDOWS_WPF
            Stream stream
#endif
            )
        {

        }

        #endregion

        #region Event

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Property

        /// <summary>
        /// 内容区
        /// </summary>
        public AcorisMarkdownContent Content {
            get {
                return _content;
            }
            set {
                _content = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Content)));
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public AcorisMarkdownHeader Header {
            get {
                return _header;
            }
            set {
                _header = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public AcorisMarkdownResourceManager Resource {
            get {
                return _resource;
            } 
        }

        /// <summary>
        /// 
        /// </summary>
        public AcorisMarkdownInteractionManager Interaction {
            get {
                return _interaction;
            }
        }

        #endregion

    }
}
